package com.acwing.partition4;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author `RKC`
 * @date 2022/1/13 14:23
 */
public class AC341最优贸易 {

    private static final int N = 100010, M = 500010 * 2 * 2, INF = 0x3f3f3f3f;
    private static int n = 0, m = 0, index = 0;
    private static int[] head = new int[N], element = new int[M], next = new int[M], weight = new int[N];
    private static int[] revHead = new int[N];
    //dist1[i]表示从1走到i的过程中，买入水晶球的最低价格；dist2[i]表示从n走到i的过程中，卖出水晶球的最高价格
    private static int[] dist1 = new int[N], dist2 = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        s = reader.readLine().split("\\s+");
        for (int i = 0; i <= n; i++) head[i] = revHead[i] = -1;
        for (int i = 1; i <= n; i++) weight[i] = Integer.parseInt(s[i - 1]);
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), type = Integer.parseInt(s[2]);
            createEdge(head, u, v);
            createEdge(revHead, v, u);
            if (type == 2) {
                createEdge(revHead, v, u);
                createEdge(revHead, u, v);
            }
        }
        //spfa算法分别计算从1->n/n->1的过程中经过点i的最小/大点权
        spfa(dist1, 1, head, 1);
        spfa(dist2, n, revHead, 2);
        int answer = 0;
        //枚举每个点作为分界点时的收益
        for (int i = 1; i <= n; i++) answer = Math.max(answer, dist2[i] - dist1[i]);
        writer.write(answer + "\n");
        writer.flush();
    }

    private static void spfa(int[] dist, int start, int[] head, int type) {
        Queue<Integer> queue = new LinkedList<>();
        boolean[] inQueue = new boolean[n + 1];
        if (type == 1) Arrays.fill(dist, INF);
        else Arrays.fill(dist, -INF);
        dist[start] = weight[start];
        queue.add(start);
        inQueue[start] = true;
        while (!queue.isEmpty()) {
            int u = queue.poll();
            inQueue[u] = false;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if ((type == 1 && dist[v] > Math.min(dist[u], weight[v])) || (type == 2 && dist[v] < Math.max(dist[u], weight[v]))) {
                    if (type == 1) dist[v] = Math.min(dist[u], weight[v]);
                    else dist[v] = Math.max(dist[u], weight[v]);
                    if (inQueue[v]) continue;
                    queue.add(v);
                    inQueue[v] = true;
                }
            }
        }
    }

    private static void createEdge(int[] head, int u, int v) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index++;
    }
}
